package com.ebupt.migu.music.autotri.controller;

import com.ebupt.migu.music.autotri.entity.DeptChangeReqBean;
import com.ebupt.migu.music.autotri.mapper.AutoOperateMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dept")
public class DeptChangeController {
    @Autowired
    private AutoOperateMapper mapper;

    @RequestMapping("/change")
    public void change(@RequestBody DeptChangeReqBean bean) {
        StringBuffer sys_user = new StringBuffer();
        sys_user.append("update sys_user set dept_id = ");
        sys_user.append(bean.getReDeptId());
        sys_user.append(", dept_name = '");
        sys_user.append(bean.getReDeptName());
        sys_user.append("'");
        switch (bean.getMode()) {
            case "1":
                // 系统用户表单独处理
                sys_user.append(" where login_name = '");
                sys_user.append(bean.getCreatorId());
                sys_user.append("'");
                log.info(String.valueOf(sys_user));
                mapper.updateDeptInfo(String.valueOf(sys_user));
                // PUSH效果统计表单独处理
                StringBuffer push = new StringBuffer();
                push.append("update statistics_push set creator_department = '");
                push.append(bean.getReDeptName());
                push.append("' WHERE strategy_id in(SELECT strategy_id from strategy_info where creator_id = '");
                push.append(bean.getCreatorId());
                push.append("')");
                log.info(String.valueOf(push));
                mapper.updateDeptInfo(String.valueOf(push));

                dept_change_tables().stream().forEach(map -> {
                    StringBuffer sb = new StringBuffer();
                    sb.append("update");
                    sb.append(" ");
                    sb.append(map.get("table_name"));
                    sb.append(" ");
                    sb.append("set");
                    sb.append(" ");
                    sb.append(map.get("dept_column"));
                    sb.append(" ");
                    sb.append("=");
                    sb.append(" ");
                    sb.append("'");
                    sb.append(bean.getReDeptName());
                    sb.append("'");
                    sb.append(" ");
                    sb.append("where");
                    sb.append(" ");
                    sb.append(map.get("creator_column"));
                    sb.append(" ");
                    sb.append("=");
                    sb.append(" ");
                    sb.append("'");
                    sb.append(bean.getCreatorId());
                    sb.append("'");
                    sb.append(";");
                    log.info(String.valueOf(sb));
                    mapper.updateDeptInfo(String.valueOf(sb));
                });
        }
    }


    private static List<HashMap<String, String>> dept_change_tables() {
        List<HashMap<String, String>> tables = new ArrayList<>();
        // 审核历史表
        HashMap<String, String> approve_history = new HashMap<>();
        approve_history.put("table_name", "approve_history");
        approve_history.put("dept_column", "creator_department");
        approve_history.put("creator_column", "creator_id");
        tables.add(approve_history);

        // 自动运营表
        HashMap<String, String> auto_operate = new HashMap<>();
        auto_operate.put("table_name", "auto_operate");
        auto_operate.put("dept_column", "creator_department");
        auto_operate.put("creator_column", "creator_id");
        tables.add(auto_operate);

        // 自动运营审核表
        HashMap<String, String> auto_operate_tac_approve = new HashMap<>();
        auto_operate_tac_approve.put("table_name", "auto_operate_tac_approve");
        auto_operate_tac_approve.put("dept_column", "creator_department");
        auto_operate_tac_approve.put("creator_column", "creator_id");
        tables.add(auto_operate_tac_approve);

        // 自动触发表
        HashMap<String, String> auto_tri_tac = new HashMap<>();
        auto_tri_tac.put("table_name", "auto_tri_tac");
        auto_tri_tac.put("dept_column", "creator_department");
        auto_tri_tac.put("creator_column", "creator_id");
        tables.add(auto_tri_tac);

        // 自动触发审核表
        HashMap<String, String> auto_tri_tac_approve = new HashMap<>();
        auto_tri_tac_approve.put("table_name", "auto_tri_tac_approve");
        auto_tri_tac_approve.put("dept_column", "creator_department");
        auto_tri_tac_approve.put("creator_column", "creator_id");
        tables.add(auto_tri_tac_approve);

        // DPI策略表
        HashMap<String, String> dpi_strategy = new HashMap<>();
        dpi_strategy.put("table_name", "dpi_strategy");
        dpi_strategy.put("dept_column", "creator_department");
        dpi_strategy.put("creator_column", "creator_id");
        tables.add(dpi_strategy);

        // 全局弹窗审核表
        HashMap<String, String> global_activity_approve = new HashMap<>();
        global_activity_approve.put("table_name", "global_activity_approve");
        global_activity_approve.put("dept_column", "creator_department");
        global_activity_approve.put("creator_column", "creator_id");
        tables.add(global_activity_approve);

        // 全局弹窗表
        HashMap<String, String> global_activity_info = new HashMap<>();
        global_activity_info.put("table_name", "global_activity_info");
        global_activity_info.put("dept_column", "creator_department");
        global_activity_info.put("creator_column", "creator_id");
        tables.add(global_activity_info);

        // 消息通知用户表
        HashMap<String, String> msg_type = new HashMap<>();
        msg_type.put("table_name", "msg_type");
        msg_type.put("dept_column", "creator_department");
        msg_type.put("creator_column", "creator_id");
        tables.add(msg_type);

        // 公播广场表
        HashMap<String, String> public_broadcast_activity_info = new HashMap<>();
        public_broadcast_activity_info.put("table_name", "public_broadcast_activity_info");
        public_broadcast_activity_info.put("dept_column", "creator_department");
        public_broadcast_activity_info.put("creator_column", "creator_id");
        tables.add(public_broadcast_activity_info);

        // 资源下线表
        HashMap<String, String> resources_offline_info = new HashMap<>();
        resources_offline_info.put("table_name", "resources_offline_info");
        resources_offline_info.put("dept_column", "creator_department");
        resources_offline_info.put("creator_column", "creator_id");
        tables.add(resources_offline_info);

        // 黑/白名单表
        HashMap<String, String> roster = new HashMap<>();
        roster.put("table_name", "roster");
        roster.put("dept_column", "creator_department");
        roster.put("creator_column", "creator_id");
        tables.add(roster);

        // 歌手黑名单表
        HashMap<String, String> singer_black_list = new HashMap<>();
        singer_black_list.put("table_name", "singer_black_list");
        singer_black_list.put("dept_column", "creator_department");
        singer_black_list.put("creator_column", "creator_id");
        tables.add(singer_black_list);

        // 短信模板表
        HashMap<String, String> sms_template = new HashMap<>();
        sms_template.put("table_name", "sms_template");
        sms_template.put("dept_column", "creator_department");
        sms_template.put("creator_column", "creator");
        tables.add(sms_template);

        // 离线策略信息表
        HashMap<String, String> strategy_info = new HashMap<>();
        strategy_info.put("table_name", "strategy_info");
        strategy_info.put("dept_column", "creator_department");
        strategy_info.put("creator_column", "creator_id");
        tables.add(strategy_info);

        // 用户审批表
        HashMap<String, String> user_approve = new HashMap<>();
        user_approve.put("table_name", "user_approve");
        user_approve.put("dept_column", "creator_department");
        user_approve.put("creator_column", "creator_id");
        tables.add(user_approve);

        // 客群信息表
        HashMap<String, String> user_group_info = new HashMap<>();
        user_group_info.put("table_name", "user_group_info");
        user_group_info.put("dept_column", "creator_department");
        user_group_info.put("creator_column", "creator_id");
        tables.add(user_group_info);

        // vip分组表
        HashMap<String, String> vip_block_info = new HashMap<>();
        vip_block_info.put("table_name", "vip_block_info");
        vip_block_info.put("dept_column", "creator_department");
        vip_block_info.put("creator_column", "creator_id");
        tables.add(vip_block_info);

        // vip红包表
        HashMap<String, String> vip_packet_info = new HashMap<>();
        vip_packet_info.put("table_name", "vip_packet_info");
        vip_packet_info.put("dept_column", "creator_department");
        vip_packet_info.put("creator_column", "creator_id");
        tables.add(vip_packet_info);

        // 融媒活动配置表
        HashMap<String, String> media_activity_config_table = new HashMap<>();
        media_activity_config_table.put("table_name", "media_activity_config_table");
        media_activity_config_table.put("dept_column", "creator_department");
        media_activity_config_table.put("creator_column", "creator_id");
        tables.add(media_activity_config_table);

        return tables;
    }
}
